from django.db import models
from Login.models import User

# 用户表
# class User(models.Model):
#     username = models.CharField(max_length=32, unique=True)  # 用户名
#     avatar = models.URLField(verbose_name='头像')             # 头像图片URL
#     level = models.IntegerField(default=1, verbose_name='等级') # 用户等级
#     points = models.IntegerField(default=0, verbose_name='积分') # 用户积分
#     # 你可以根据需要添加更多用户属性

# 好友表
class Friend(models.Model):
    user = models.ForeignKey(User, related_name='owner', on_delete=models.CASCADE)
    friend = models.ForeignKey(User, related_name='friend', on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)  # 成为好友的时间

# 排行表（每周）
class ActivityRank(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    week = models.DateField()  # 周的起始日期
    score = models.IntegerField()  # 积分/排名分数

# 动态表
class UserActivity(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()  # 动态内容
    image = models.URLField(blank=True, null=True)  # 动态图片
    created_at = models.DateTimeField(auto_now_add=True)

# 训练数据表
class TrainingData(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    duration = models.IntegerField()  # 时长（分钟）
    calories = models.FloatField()    # 卡路里
    heart_rate = models.IntegerField()  # 心率
    count = models.IntegerField()     # 完成次数
    date = models.DateField()

# 互动表
class Interaction(models.Model):
    user = models.ForeignKey(User, related_name='from_user', on_delete=models.CASCADE)
    target = models.ForeignKey(User, related_name='to_user', on_delete=models.CASCADE)
    type = models.CharField(max_length=16)  # 互动类型：关注/组队等
    created_at = models.DateTimeField(auto_now_add=True)

# 推荐内容表
class Recommendation(models.Model):
    title = models.CharField(max_length=64)  # 推荐标题
    description = models.TextField(blank=True, null=True)  # 推荐描述
    image = models.URLField()  # 推荐图片
    link = models.CharField(max_length=128)  # 跳转链接
    type = models.CharField(max_length=32)  # 推荐类型：身体评估/课程/动作等
    created_at = models.DateTimeField(auto_now_add=True)

# 课程表
class Course(models.Model):
    name = models.CharField(max_length=64)  # 课程名称
    difficulty = models.CharField(max_length=16)  # 难度
    part = models.CharField(max_length=32)        # 部位
    duration = models.IntegerField()              # 时长
    property = models.CharField(max_length=32)    # 性质
    feature = models.CharField(max_length=32)     # 动作特点
    equipment = models.CharField(max_length=32)   # 器械

# 课程分类表
class CourseCategory(models.Model):
    name = models.CharField(max_length=32)  # 分类名称

# 课程详情表
class CourseDetail(models.Model):
    course = models.OneToOneField(Course, on_delete=models.CASCADE)
    difficulty = models.CharField(max_length=16)  # 难度
    score = models.FloatField()                   # 评分
    duration = models.IntegerField()              # 时长

# 顶部/底部导航栏模型
class NavTab(models.Model):
    name = models.CharField(max_length=16, verbose_name='导航名称')  # 导航栏名称，如"首页"
    order = models.IntegerField(default=0, verbose_name='排序')      # 显示顺序
    icon = models.CharField(max_length=64, verbose_name='图标', blank=True, null=True) # 图标（可用iconfont类名或图片URL）
    link = models.CharField(max_length=128, verbose_name='跳转链接', blank=True, null=True) # 跳转链接
    position = models.CharField(
        max_length=16, 
        choices=[('top', '顶部'), ('bottom', '底部')], 
        default='top'
    )  # 位置，顶部或底部

# 首页推荐内容（Feed流/卡片）模型
class FeedCard(models.Model):
    CARD_TYPE_CHOICES = [
        ('event', '活动/赛事'),
        ('product', '商品'),
        ('ad', '广告'),
        ('news', '资讯'),
    ]
    title = models.CharField(max_length=64)                  # 卡片标题
    description = models.TextField(blank=True, null=True)    # 卡片描述
    image = models.URLField()                                # 卡片图片
    card_type = models.CharField(max_length=16, choices=CARD_TYPE_CHOICES) # 卡片类型
    link = models.CharField(max_length=128, blank=True, null=True)         # 跳转链接
    start_time = models.DateTimeField(blank=True, null=True) # 活动开始时间
    end_time = models.DateTimeField(blank=True, null=True)   # 活动结束时间
    participants = models.IntegerField(default=0)            # 参与人数
    tag = models.CharField(max_length=16, blank=True, null=True) # 卡片标签，如"奖牌赛"
    order = models.IntegerField(default=0)                   # 排序

# 搜索热词模型（可选）
class SearchHotword(models.Model):
    word = models.CharField(max_length=32, unique=True)      # 热词内容
    count = models.IntegerField(default=0)                   # 搜索次数

# 分类标签模型（如"躺床练"、"奖牌赛"等）
class CategoryTag(models.Model):
    name = models.CharField(max_length=16, unique=True)      # 标签名称
    order = models.IntegerField(default=0)                   # 排序

# Feed卡片与标签的多对多关系
class FeedCardTag(models.Model):
    feed_card = models.ForeignKey(FeedCard, on_delete=models.CASCADE) # 关联的Feed卡片
    tag = models.ForeignKey(CategoryTag, on_delete=models.CASCADE)    # 关联的标签

# 首页-社区用户动态模型
class CommunityPost(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='发布用户')
    title = models.CharField(max_length=64, verbose_name='动态标题')
    content = models.TextField(verbose_name='正文内容')
    images = models.JSONField(default=list, verbose_name='图片列表')  # 存图片url数组
    duration = models.CharField(max_length=16, verbose_name='训练时长')
    pace = models.CharField(max_length=16, verbose_name='平均配速')
    calorie = models.CharField(max_length=16, verbose_name='运动消耗')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='发布时间')

    class Meta:
        verbose_name = '社区动态'
        verbose_name_plural = '社区动态'

    def __str__(self):
        return f"{self.user.username} - {self.title}"

class Video(models.Model):
    title = models.CharField(max_length=128)
    cover = models.URLField()
    duration = models.CharField(max_length=16)
    author = models.CharField(max_length=64)
    author_avatar = models.URLField()
    views = models.CharField(max_length=16)
    time = models.CharField(max_length=32)
    video_url = models.URLField()

    def __str__(self):
        return self.title

class Comment(models.Model):
    post = models.ForeignKey(CommunityPost, on_delete=models.CASCADE, related_name='comments')
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField()
    parent = models.ForeignKey('self', null=True, blank=True, on_delete=models.CASCADE, related_name='replies')
    created_at = models.DateTimeField(auto_now_add=True)

class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(CommunityPost, null=True, blank=True, on_delete=models.CASCADE, related_name='likes')
    comment = models.ForeignKey('Comment', null=True, blank=True, on_delete=models.CASCADE, related_name='likes')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = (('user', 'post'), ('user', 'comment'))

class UserVideoHistory(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    video = models.ForeignKey(Video, on_delete=models.CASCADE)
    viewed_at = models.DateTimeField(auto_now=True)
    class Meta:
        unique_together = ("user", "video")
        ordering = ["-viewed_at"]